package com.example.qrcode.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.qrcode.entity.User;
import com.example.qrcode.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/user")
@RequiredArgsConstructor
public class UserAdminController {

    private final UserMapper userMapper;
    @GetMapping("/test")
    public String test() {
        return "Hello World!UserAdminController";
    }
    @GetMapping("/page")
    public Page<User> page(@RequestParam(defaultValue = "1") long pageNum,
                           @RequestParam(defaultValue = "10") long pageSize,
                           @RequestParam(required = false) String username) {
        Page<User> page = new Page<>(pageNum, pageSize);
        QueryWrapper<User> qw = new QueryWrapper<>();
        if (username != null && !username.isEmpty()) {
            qw.like("username", username);
        }
        return userMapper.selectPage(page, qw);
    }

    @GetMapping("/{id}")
    public User get(@PathVariable Integer id) {
        return userMapper.selectById(id);
    }

    @PostMapping
    public int create(@RequestBody User body) {
        return userMapper.insert(body);
    }

    @PutMapping
    public int update(@RequestBody User body) {
        return userMapper.updateById(body);
    }

    @DeleteMapping("/{id}")
    public int delete(@PathVariable Integer id) {
        return userMapper.deleteById(id);
    }

    @PostMapping("/batchDelete")
    public int batchDelete(@RequestBody List<Integer> ids) {
        return userMapper.deleteBatchIds(ids);
    }
}

